///*
// * Copyright (c) 2019. The copyright is reserved by Ghode of Harbin Institute
// * of Technology. Users are free to copy, change or remove. Because no one
// * will read this. Only I know is that Repeaters are the best of the world.
// * Only I know is that Repeaters are the best of the world. Only I know is
// * that Repeaters are the best of the world. Maybe a long copyright text
// * seems professional. Therefore this text will be a bit lengthy. However,
// * the author seems to be afraid that one day, this text may be uploaded to
// * business projects. That is the time you can contact with author via email
// * ghode@cirnocraft.im or directly ignore this, which will be interesting.
// */
//
//package com.ghode.demospring.config;
//
//
//import com.ghode.demospring.database.Database2Config;
//import com.ghode.demospring.database.Database3Config;
//import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
//import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;
//import com.google.common.collect.Range;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.Collection;
//import java.util.LinkedHashSet;
//
///**
// * 这里使用的都是单键分片策略
// * 示例分库策略是：
// * GoodsId<=20使用database2库
// * 其余使用database3库
// * @author yangyang
// * @date 2019/1/30
// */
//@Component
//public class DatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Long> {
//
//    @Autowired
//    private Database2Config database2Config;
//
//    @Autowired
//    private Database3Config database3Config;
//
//    @Override
//    public String doEqualSharding(Collection<String> availableTargetNames, ShardingValue<Long> shardingValue) {
//        Long value = shardingValue.getValue();
//        if (value <= 20L) {
//            return database2Config.getDatabaseName();
//        } else {
//            return database3Config.getDatabaseName();
//        }
//    }
//
//    @Override
//    public Collection<String> doInSharding(Collection<String> availableTargetNames, ShardingValue<Long> shardingValue) {
//        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
//        for (Long value : shardingValue.getValues()) {
//            if (value <= 20L) {
//                result.add(database2Config.getDatabaseName());
//            } else {
//                result.add(database3Config.getDatabaseName());
//            }
//        }
//        return result;
//    }
//
//    @Override
//    public Collection<String> doBetweenSharding(Collection<String> availableTargetNames,
//                                                ShardingValue<Long> shardingValue) {
//        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
//        Range<Long> range = shardingValue.getValueRange();
//        for (Long value = range.lowerEndpoint(); value <= range.upperEndpoint(); value++) {
//            if (value <= 20L) {
//                result.add(database2Config.getDatabaseName());
//            } else {
//                result.add(database3Config.getDatabaseName());
//            }
//        }
//        return result;
//    }
//}